package com.lkx.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkx.dao.ApiMapper;
import com.lkx.dao.ApiParamsMapper;
import com.lkx.domain.Api;
import com.lkx.domain.ApiAndType;
import com.lkx.domain.ApiParams;
import com.lkx.service.ApiService;
import com.lkx.util.PageModule;

@Service
public class ApiServiceImpl implements ApiService {

    private Logger logger = LoggerFactory.getLogger(ApiServiceImpl.class);

    @Autowired
    private ApiMapper apiMapper;
    @Autowired
    private ApiParamsMapper apiParamsMapper;

    @Override
    @Transactional
    public PageModule add(Api record, String params) {
        PageModule module = new PageModule();
        try {
            int num1 = apiMapper.insert(record);
            List<ApiParams> paramList = new ArrayList<ApiParams>();
            paramList = JSON.parseArray(params, ApiParams.class);
            for (ApiParams apiParams : paramList) {
                apiParams.setApiId(record.getId());
            }
            int num2 = apiParamsMapper.batchInsert(paramList);
            if (num1 > 0 && num2 > 0) {
                module.setStatus(true);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return module;
    }

    @Override
    @Transactional
    public PageModule edit(Api record, String params) throws Exception {
        PageModule module = new PageModule();
        try {
            int num1 = apiMapper.updateByPrimaryKeySelective(record);
            List<ApiParams> paramList = new ArrayList<ApiParams>();
            paramList = JSON.parseArray(params, ApiParams.class);
            for (ApiParams apiParams : paramList) {
                apiParams.setApiId(record.getId());
            }
            int num2 = apiParamsMapper.deleteByApiId(record.getId());

            int num3 = apiParamsMapper.batchInsert(paramList);

            if (num1 > 0 && num2 > 0 && num3 > 0) {
                module.setStatus(true);
            }

        } catch (Exception e) {
            throw new Exception("保存异常:" + e.getMessage());
        }
        return module;
    }

    @Override
    public PageModule list(Long userId, String apiName, Long subjectId, int pageNum, int pageSize) {
        PageModule module = new PageModule();
        try {
            Page page = PageHelper.startPage(pageNum, pageSize);
            Map<String, Object> map = new HashMap();
            map.put("userId", userId);
            map.put("apiName", apiName);
            map.put("apiSubjectId", subjectId);
            List<Api> list = new ArrayList<Api>();
            list = apiMapper.selectListByOption(map);
            if (list != null && list.size() > 0) {
                module.setStatus(true);
                module.setTotal(page.getTotal());
                module.setList(list);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return module;
    }

    @Override
    public Api selectByPrimaryKey(Long id) {
        return apiMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ApiAndType> selectApiListByOption(Long userId, Long subjectId) {
        try {
            Map<String, Object> map = new HashMap();
            map.put("userId", userId);
            map.put("subjectId", subjectId);
            return apiMapper.selectApiListByOption(map);
        } catch (Exception e) {
            return null;
        }

    }

    @Transactional
    @Override
    public PageModule deleteById(Long id) {
        PageModule module = new PageModule();

        int num1 = apiMapper.deleteByPrimaryKey(id);
        int num2 = apiParamsMapper.deleteByApiId(id);
        if (num1 > 0 && num2 > 0) {
            module.setStatus(true);
        }
        return module;
    }


}
